package cn.itcast.order.controller;

import cn.itcast.feign.client.UserClient;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.threadLocal.ThreadLocalUtil;
import feign.Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("address")
@Slf4j
public class TestController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    UserClient userClient;
//    @GetMapping("/{uid}")
    @GetMapping("/uid")
//    public List<Address> test(@PathVariable("uid") String uid){
    public List<Address> test(){
        Long userId = ThreadLocalUtil.getUserId();
        List<Address> byId = userClient.findById(userId.toString());
        System.out.println("test...hello"+userId);
        return byId;
    }

    @GetMapping("/mq")
    public void testMQ(){
        // 创建消息
//        Message message = MessageBuilder
//                .withBody("hello, ttl message".getBytes(StandardCharsets.UTF_8))
//                .setExpiration("5000")
//                .build();
//        // 消息ID，需要封装到CorrelationData中
//        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
//        // 发送消息
//        rabbitTemplate.convertAndSend("ttl.direct", "ttl", message, correlationData);
//        log.debug("发送消息成功");

        // 创建消息
        String message = "hello, ttl queue";
        // 消息ID，需要封装到CorrelationData中
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        // 发送消息
        rabbitTemplate.convertAndSend("ttl.direct", "ttl", message, correlationData);
        // 记录日志
        log.info("发送消息成功");
        /**
         *

        Long orderId = "*****";
        // 消息ID，需要封装到CorrelationData中
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        // 发送消息
        rabbitTemplate.convertAndSend("ttl.direct", "ttl", orderId, correlationData);
        // 记录日志
        log.info("发送消息成功");

         */
    }

}
